package com.productline;

import java.io.File;

import org.apple.util.Configure;
import org.apple.util.FileUtil;
import org.apple.util.PLEConfigure;

public class SrcGenerator {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		SrcGenerator sg = new SrcGenerator(PLEConfigure.instance(),"F:\\product\\");
		//sg.generateSrc();
		sg.copySrc("org.apche.pas");
	}
	private static PLEConfigure conf;
	
	private String targetDir;
	private String srcDir;
	private String optionalDir;
	public SrcGenerator(PLEConfigure conf, String targetDir){
		this.conf = conf;
		//targetDir = conf.getProperty("targetDir");
		this.targetDir = targetDir;
		System.out.println("======"+targetDir+"=====");
		File nSrc = new File("sourcecodebase"+java.io.File.separator+"necessary");
		if(nSrc.exists()){
			System.out.println(nSrc.getAbsolutePath());
			srcDir = nSrc.getAbsolutePath()+java.io.File.separator;
		}else{
			srcDir = conf.getProperty("srcDir");
		}
		File oSrc = new File("sourcecodebase"+java.io.File.separator+"optional");
		if(nSrc.exists()){
			System.out.println(oSrc.getAbsolutePath());
			optionalDir = oSrc.getAbsolutePath()+java.io.File.separator;
		}else{
			optionalDir = conf.getProperty("optionalDir");
		}
	}
	
	public void generateSrc(){
		FileUtil.deleteFile(targetDir);
		FileUtil.copyFileFromDir(targetDir+"src"+java.io.File.separator, srcDir, false);
		String collectWS = conf.getProperty("collectWindSpeed");
		String collectAT = conf.getProperty("collectAirTemperature");
		String collectST = conf.getProperty("collectSeaTemperature");
		String collectPS = conf.getProperty("collectPosition");
		String SE = java.io.File.separator;
		if("true".equals(collectWS)){
			String windSpeedSensorType  = conf.getProperty("windSpeedSensorType");
			this.copySrc(windSpeedSensorType);
			this.copySrc("org"+SE+"apple"+SE+"sensor"+SE+"monitor"+SE+"WindSpeedSensorMonitor");
			this.copySrc("org"+SE+"apple"+SE+"averager"+SE+"WindSpeedAverager");
		}
		if("true".equals(collectAT)){
			String airTempratureSensorType   = conf.getProperty("airTempratureSensorType");
			this.copySrc(airTempratureSensorType);
			this.copySrc("org"+SE+"apple"+SE+"sensor"+SE+"monitor"+SE+"AirTempratureSensorMonitor");
			this.copySrc("org"+SE+"apple"+SE+"averager"+SE+"AirTemperatureAverager");
		}
		if("true".equals(collectST)){
			String seaTempratureSensorType   = conf.getProperty("seaTempratureSensorType");
			this.copySrc(seaTempratureSensorType);
			this.copySrc("org"+SE+"apple"+SE+"sensor"+SE+"monitor"+SE+"SeaTempratureSensorMonitor");
			this.copySrc("org"+SE+"apple"+SE+"averager"+SE+"SeaTemperatureAverager");
		}
		if("true".equals(collectPS)){
			String positionSensorType   = conf.getProperty("NavigationDriver");
			this.copySrc(positionSensorType);
		}
		
		String messageFormat = conf.getProperty("messageFormat");
		this.copySrc(messageFormat);
		
		String messageGenrator = conf.getProperty("messageGenrator");
		this.copySrc(messageGenrator);
		
		String transmitterType = conf.getProperty("transmitterType");
		this.copySrc(transmitterType);
		
		String dataBankerType = conf.getProperty("dataBankerType");
		this.copySrc(dataBankerType);
		
		FileUtil.copyFile(new File(targetDir+"src"+SE+"apple.properties"), new File("apple.properties"), true);
		
		FileUtil.copyFile(new File(targetDir+"build.xml"), new File("build-jar.xml"), true);
	}
	
	public void copySrc(String classType){
		System.out.println(classType);
		classType = classType.replaceAll("\\.", "\\\\");
		System.out.println(classType);
		FileUtil.copyFile(new File(targetDir+"src"+java.io.File.separator+classType+".java"), new File(optionalDir+classType+".java"), true);
	}
}
